// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.Classes
{
	using System.Drawing;
	using System.IO;

	using Microsoft.Win32;

	/// <summary>
	/// A Class with a list of Common Static Methods...
	/// </summary>
	public class Common
	{
		#region Methods

		/// <summary>
		/// Gets the Shell Icon for the given file...
		/// </summary>
		/// <param name="name">Path to file.</param>
		/// <param name="linkOverlay">Shortcut Overlay or not</param>
		/// <returns>Icon</returns>
		public static System.Drawing.Icon GetFileIcon(string name, bool linkOverlay)
		{
			cShell32.SHFILEINFO shfi = new cShell32.SHFILEINFO();
			uint flags = cShell32.SHGFI_ICON | cShell32.SHGFI_USEFILEATTRIBUTES;

			if (linkOverlay) flags += cShell32.SHGFI_LINKOVERLAY;
			flags += cShell32.SHGFI_SMALLICON; // include the small icon flag

			cShell32.SHGetFileInfo(name,
			                       cShell32.FILE_ATTRIBUTE_NORMAL,
			                       ref shfi,
			                       (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
			                       flags);

			// Copy (clone) the returned icon to a new object, thus allowing us
			// to call DestroyIcon immediately
			System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
			User32.DestroyIcon(shfi.hIcon); // Cleanup
			return icon;
		}

		public static StringFormat StringFormatAlignment(ContentAlignment textalign)
		{
			StringFormat sf = new StringFormat();
			switch (textalign) {
			    case ContentAlignment.TopLeft:
			    case ContentAlignment.TopCenter:
			    case ContentAlignment.TopRight:
				    sf.LineAlignment = StringAlignment.Near;
				    break;
			    case ContentAlignment.MiddleLeft:
			    case ContentAlignment.MiddleCenter:
			    case ContentAlignment.MiddleRight:
				    sf.LineAlignment = StringAlignment.Center;
				    break;
			    case ContentAlignment.BottomLeft:
			    case ContentAlignment.BottomCenter:
			    case ContentAlignment.BottomRight:
				    sf.LineAlignment = StringAlignment.Far;
				    break;
			}
			switch (textalign) {
			    case ContentAlignment.TopLeft:
			    case ContentAlignment.MiddleLeft:
			    case ContentAlignment.BottomLeft:
				    sf.Alignment = StringAlignment.Near;
				    break;
			    case ContentAlignment.TopCenter:
			    case ContentAlignment.MiddleCenter:
			    case ContentAlignment.BottomCenter:
				    sf.Alignment = StringAlignment.Center;
				    break;
			    case ContentAlignment.TopRight:
			    case ContentAlignment.MiddleRight:
			    case ContentAlignment.BottomRight:
				    sf.Alignment = StringAlignment.Far;
				    break;
			}
			return sf;
		}

		#endregion Methods

		#region Nested Types

		/// <summary>
		/// Struct for the configuration settings for a editor...
		/// </summary>
		public struct EditorConfig
		{
			#region Fields

			public bool AllowCaretBeyondEOL;
			public bool AutoInsertBracket;
			public string BracketMatchingStyle;
			public bool ConvertTabs;
			public Font EditorFont;
			public bool EnableCodeFolding;
			public bool EnableCompletion;
			public bool HighlightCurrentLine;
			public string IndentStyle;
			public bool ShowEOL;
			public bool ShowHRuler;
			public bool ShowInvalidLines;
			public bool ShowLineNumbers;
			public bool ShowMatchingBracket;
			public bool ShowSpaces;
			public bool ShowTabs;
			public bool ShowVRuler;
			public int TabIndent;
			public bool UseAntiAlias;
			public int VerticalRulerCol;
            public Color BracketHighlightOutlineColor;
            public Color BracketHighlightInnerColor;

			#endregion Fields
		}

		#endregion Nested Types
	}

	/*
	    FileTypeRegistrar fileReg = new FileTypeRegistrar();
	    {
	        fileReg.FullPath = Path_To_Executable;
	        fileReg.FileExtension = Extension_To_Register;
	        fileReg.ContentType = "application/" + Your_Description;
	        fileReg.IconIndex = Icon_Index_In_Application;
	        fileReg.IconPath = Path_To_Executable;
	        fileReg.ProperName = Name_Of_Executable;
	        fileReg.CreateType();
	    }
	 */
	public class FileTypeRegistrar
	{
		#region Fields

		private string _ContentType;
		private string _FileExtension;
		private string _FullPath;
		private int _IconIndex;
		private string _IconPath;
		private string _ProperName;

		#endregion Fields

		#region Properties

		public string ContentType
		{
			get { return _ContentType; }
			set { _ContentType = value; }
		}

		public string FileExtension
		{
			get { return _FileExtension; }
			set { _FileExtension = value.Replace(".", ""); }
		}

		public string FullPath
		{
			get { return _FullPath; }
			set { _FullPath = value; }
		}

		public int IconIndex
		{
			get { return _IconIndex; }
			set { _IconIndex = value; }
		}

		public string IconPath
		{
			get { return _IconPath; }
			set { _IconPath = value; }
		}

		public string ProperName
		{
			get { return _ProperName; }
			set { _ProperName = value; }
		}

		#endregion Properties

		#region Methods

		public void CreateType()
		{
			string fileName = Path.GetFileNameWithoutExtension(FullPath);
			string Ext = "." + FileExtension.ToLower();
			RegistryKey extKey = Registry.ClassesRoot.CreateSubKey(Ext);

			extKey.SetValue("", fileName);
			extKey.SetValue("Content Type", ContentType);
			extKey.Close();

			RegistryKey mainKey = Registry.ClassesRoot.CreateSubKey(fileName);
			RegistryKey defIconKey = mainKey.CreateSubKey("DefaultIcon");

			defIconKey.SetValue("", IconPath + ", " + IconIndex);
			defIconKey.Close();

			RegistryKey shellKey = mainKey.CreateSubKey("shell");
			RegistryKey OpenKey = shellKey.CreateSubKey("Open");
			RegistryKey cmdKey = OpenKey.CreateSubKey("command");

			cmdKey.SetValue("", "\"" + FullPath + " %1\"");
			cmdKey.Close();
			OpenKey.Close();
			shellKey.Close();
			mainKey.Close();
		}

		public void DeleteType()
		{
			string fileName = Path.GetFileNameWithoutExtension(FullPath);
			string Ext = "." + FileExtension.ToLower();

			Registry.ClassesRoot.DeleteSubKey(Ext);
			Registry.ClassesRoot.DeleteSubKey(fileName);
		}

		#endregion Methods
	}
}